<?php

namespace hook;
use core\Request;
use core\Response;
use extend\Jwt;

class CheckPre
{
    public static function run(&$class)
    {
        $pre = isset($class::$_pre) ? $class::$_pre : [];

        if ( in_array('login',$pre) ) {
            ( $token = Request::get('header.token') ) || Response::send(401,'token必须');
            ( $payload = Jwt::decode($token) ) || Response::send(401,'token验证失败');
            $payload['exp'] < time() && Response::send(401,'token已过期');

            // 载荷写入请求类
            Request::set('payload',$payload);
        }

    }
}
